-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[RFC] rustc_parse: improve the error diagnostic for "missing let in let chain" #151493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
r? @chenyukang rustbot has assigned @chenyukang. Use |
|
My approach seems not to be perfect and I have been stucked on this for a while. This is the issue this is supposed to fix - >#117977 |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
6b5f379 to
f1467c2
Compare
|
r? @estebank |
|
This is an improvement for initially it gives but now it gives |
|
Interestingly, still give the same result. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I follow your last comment. The diagnostic you're showing doesn't correspond with the code. I think you meant the following?
fn main() {
let x = Some(42);
if Some(_) = x
&& let Some(x) = x
{}
}If so, this is the output I see:
error: expected expression, found `let` statement
--> foo.rs:4:12
|
4 | && let Some(x) = x
| ^^^
|
= note: -Ztrack-diagnostics: created at /rustc-dev/4fa80a5e733e2202d7ca4c203c2fdfda41cfe7dc/compiler/rustc_parse/src/parser/expr.rs:2775:43
= note: only supported directly in conditions of `if` and `while` expressions
error[E0308]: mismatched types
--> foo.rs:3:18
|
3 | if Some(_) = x
| ^ expected `bool`, found `Option<{integer}>`
|
= note: -Ztrack-diagnostics: created at /rustc-dev/4fa80a5e733e2202d7ca4c203c2fdfda41cfe7dc/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs:1905:35
= note: expected type `bool`
found enum `Option<{integer}>`
error[E0308]: mismatched types
--> foo.rs:3:8
|
3 | if Some(_) = x
| ________^^^^^^^___-
| | |
| | expected `bool`, found `Option<_>`
4 | | && let Some(x) = x
| |__________________________- this expression has type `bool`
|
= note: -Ztrack-diagnostics: created at /rustc-dev/4fa80a5e733e2202d7ca4c203c2fdfda41cfe7dc/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs:1905:35
= note: expected type `bool`
found enum `Option<_>`
error[E0308]: mismatched types
--> foo.rs:3:8
|
3 | if Some(_) = x
| ________^
4 | | && let Some(x) = x
| |__________________________^ expected `bool`, found `()`
|
= note: -Ztrack-diagnostics: created at /rustc-dev/4fa80a5e733e2202d7ca4c203c2fdfda41cfe7dc/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs:1905:35
help: consider adding `let`
|
3 | if let Some(_) = x
| +++
And I think it's coming from a different part of the parser, in parse_expr_let, so it would require different specific handling.
| help: a function with a similar name exists | ||
| | | ||
| LL - if (i + j) = i {} | ||
| LL + if (a + j) = i {} | ||
| | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated: Noticing this suggestion and it is quite bad :-/
| error: binary assignment operation `+=` cannot be used in a let chain | ||
| --> $DIR/let-chains-assign-add-incorrect.rs:8:31 | ||
| | | ||
| LL | if let _ = 1 && true && y += 2 {}; | ||
| | ---------------------- ^^ cannot use `+=` in a let chain | ||
| | | | ||
| | you are add-assigning the right-hand side expression to the result of this let-chain | ||
| | | ||
| help: you might have meant to compare with `==` instead of assigning with `+=` | ||
| | | ||
| LL - if let _ = 1 && true && y += 2 {}; | ||
| LL + if let _ = 1 && true && y == 2 {}; | ||
| | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change makes me think that we might want to restrict the silencing to cases where we had any suggestions. The need to remove the run-rustfix generally points at us giving less ideal output. That being said, this code as is is not great to begin with, and == would likely be intended... Can you see if we can make it so that we don't cause changes on this output while still silencing the others?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that is valid. Looks quite weird to me also, I knew we had to do some restrictions, I will do that.
| missing_let: self.missing_let, | ||
| comparison: self.comparison, | ||
| }); | ||
| self.found_incorrect_let_chain = Some(guar); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think all you'd have to do is gate this behind missing_let or comparison being Some.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, thanks. I did gate it earlier, but not around Some parts, so it didn’t work. I’ll gate it around it now.
Yeah, I meant this. Yeah, it needs to be handle in different part of the code. |
No description provided.